﻿using yao.Entity;
using yao.Mvc;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Titan;

namespace yao.website.Controllers
{
    /// <summary>
    /// 仓库
    /// </summary>
    public class StoreController:BaseController
    {
        public ActionResult Index(int? t, int? p)
        {
            int pageIndex = p == null ? 1 : (int)p;
            ViewBag.PageIndex = pageIndex;

            using (IDbSession cn = OpenSession())
            {
                QueryExpression query = new QueryExpression();
                query.EntityType = typeof(Store);
                query.Selects.Add(Store_.StoreId);
                query.Selects.Add(Store_.StoreName);//仓库名称
                query.Selects.Add(Store_.IsMapped);//是否已标记地图
                query.Selects.Add(Store_.IsActive);//是否启用
                query.Selects.Add(Store_.CreateTime);//创建时间
                query.Selects.Add(Store_.EmployeeId);//修改人
                query.Wheres.Add(Store_.CompanyId.TEqual(CurrentEmployee.CompanyId));
                query.PageIndex = pageIndex;
                query.PageSize = 20;
                query.ReturnMatchedCount = true;
                query.OrderBy(Store_.StoreId.Desc);
                Stores stores = new Stores();
                stores.TotalCount = cn.SelectCollection(stores.Items, query);
                return View(stores);
            }
        }

        #region 编辑
        public ActionResult Edit(long? id)
        {
            Store store = new Store();
            if (id != null)
            {
                ViewBag.Title = "修改仓库-" + id;
                store.StoreId = (long)id;
                using (IDbSession cn = OpenSession())
                {
                    cn.Select(store);
                }
            }
            else {
                store.IsActive = true;
            }
            return View(store);
        }

        [HttpPost]
        public ActionResult Edit(long? id, Store store)
        {
            Validate(store, Store_.StoreName);//仓库名必填

            using (IDbSession cn = OpenSession())
            {
                if (id == null)
                {
                    //新建
                    store.CompanyId = CurrentEmployee.CompanyId;
                    store.EmployeeId = CurrentEmployee.EmployeeId;
                    store.CreateTime = DateTime.Now;
                    cn.Insert(store);
                }
                else
                {
                    //修改
                    store.UpdateTime = DateTime.Now;
                    PropertyExpression[] propertyExpression ={
                        Store_.StoreName,
                        Store_.IsMapped,
                        Store_.X,
                        Store_.Y,
                        Store_.UpdateTime,
                        Store_.EmployeeId,
                        Store_.Remark
                    };
                    cn.Update(store, propertyExpression);
                }
            }
            return new IframeResult(Url.Action("Edit"), Url.Action("Index"));
        }
        #endregion

        #region 删除
        [HttpPost]
        public ActionResult Delete(long id)
        {
            bool ret = false;
            string message = string.Empty;

            using (IDbSession cn = OpenSession())
            {
                #region validate
                //查询仓库下，是否有商品
                ConditionExpressionCollection cs = new ConditionExpressionCollection();
                cs.Add(StoreProduct_.StoreId.TEqual(id));
                if (cn.Exists<StoreProduct>(cs))
                {
                    message = "不能删除该仓库：该仓库下还有商品";
                    return LJson(ret, message);
                }

                ConditionExpressionCollection cs1 = new ConditionExpressionCollection();
                cs1.Add(SalesOrder_.StoreId.TEqual(id));
                if (cn.Exists<SalesOrder>(cs1))
                {
                    message = "不能删除该仓库：该仓库下还有订单";
                    return LJson(ret, message);
                }
                #endregion

                Store store = new Store { StoreId = id };
                ret = cn.Delete(store) > 0;
            }
            return LJson(ret, message);
        }

        #endregion

        #region 线下启用

        [HttpPost]
        public ActionResult Active(long id, bool isActive)
        {
            Store store = new Store
            {
                StoreId=id,
                IsActive = isActive
            };
            bool ret = false;

            using (IDbSession cn = OpenSession())
            {
                ret = cn.Update(store, Store_.IsActive) > 0;
            }
            return LJson(ret,"操作成功");
        }
        #endregion
    }
}
